package com.tanhua.server.service;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.UserLocation;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.api.mongo.UserLocationApi;
import com.tanhua.server.interceptors.UserHolder;
import com.tanhua.vo.NearUserVo;
import com.tanhua.vo.UserLocationVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BaiduService {


    @Reference
    private UserLocationApi userLocationApi;
    @Reference
    private UserInfoApi userInfoApi;

    public ResponseEntity saveLocation(Double longitude, Double latitude, String addrStr) {

//        UserLocation userLocation = new UserLocation();
        userLocationApi.saveLocation(UserHolder.getUserId(), longitude,  latitude,  addrStr);

        return ResponseEntity.ok(null);
    }

    public ResponseEntity search(String gender, Integer distance) {
//        查询 UserLocation
        List<UserLocationVo> userLocationList = userLocationApi.search(UserHolder.getUserId(),distance); //TODO 有坑
//        再根据 UserLocation查询UserInfo

        List<NearUserVo> list = new ArrayList<>();
        for (UserLocationVo userLocation : userLocationList) {
            Long userId = userLocation.getUserId();
//            排除自己
            if(UserHolder.getUserId().longValue() == userId.longValue()){
                continue;
            }
            UserInfo userInfo = userInfoApi.findById(userId);
            //            处理性别
//            userInfo.getGender()
            if(!StringUtils.equals( userInfo.getGender(),gender)){
                continue;
            }

//        再根据UserInfo构建NearUserVo
            NearUserVo nearUserVo = new NearUserVo();
            nearUserVo.setUserId(userId);
            nearUserVo.setAvatar(userInfo.getAvatar());
            nearUserVo.setNickname(userInfo.getNickname());
            list.add(nearUserVo);
        }
        return ResponseEntity.ok(list);




    }
}
